package GUI.gameWin;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

import GUI.gameWinOld.CardButton;

public class CardVerticalLayout implements LayoutManager {
	private int csvpx;
	private int minHeight = 0;
	private int preferredWidth = 110, preferredHeight = 0;
	private boolean sizeUnknown = true;
	
	public CardVerticalLayout() {
		this(12);
	}

	public CardVerticalLayout(int csvpx) {
		this.csvpx = csvpx;
	}
	
	private void setSizes(Container parent) {
		int compCount = parent.getComponentCount();
		int cardCount = 0;
		for (int i = 0; i < compCount; i++) {
			Component c = parent.getComponent(i);
			if (c instanceof CardButton) {
				cardCount++;
			}
		}
		minHeight = 104 + (cardCount - 1) * csvpx;
		preferredHeight = minHeight;

	}
	
	@Override
	public void addLayoutComponent(String arg0, Component arg1) {

	}

	@Override
	public void layoutContainer(Container parent) {
		if (sizeUnknown)
			setSizes(parent);
		int compCount = parent.getComponentCount();
		int cardsHeight = (compCount-1)*csvpx + 84;
		int horOS=10;
		int verOS;
		if(parent.getHeight()>cardsHeight)
			verOS = (parent.getHeight()-cardsHeight)/2;
		else
			verOS = 10;

		CardButton lastCard = null;
		for (int i = 0; i < compCount; i++) {
			Component c = parent.getComponent(i);
			if(c instanceof CardButton) {
				lastCard = (CardButton) c;
				Dimension d = c.getPreferredSize();
				c.setBounds(horOS, verOS, d.width, csvpx);
				verOS+=csvpx;
			}
		}
		if (lastCard != null)
			lastCard.setBounds(horOS, verOS-csvpx, lastCard.getPreferredSize().width, lastCard.getPreferredSize().height);
	}

	@Override
	public Dimension minimumLayoutSize(Container parent) {
		return preferredLayoutSize(parent);
	}

	@Override
	public Dimension preferredLayoutSize(Container parent) {
		Dimension dim = new Dimension(0, 0);
		setSizes(parent);

		Insets insets = parent.getInsets();
		dim.width = preferredWidth + insets.left + insets.right;
		dim.height = preferredHeight + insets.bottom + insets.top;

		sizeUnknown = false;
		return dim;
	}

	@Override
	public void removeLayoutComponent(Component arg0) {

	}

}
